package cn.dankal.flyaudio.adapter;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.basiclib.pojo.AddressEntity;
import cn.dankal.flyaudio.personal.AddAddressActivity;
import cn.dankal.flyaudio.personal.AddressManagerActivity;
import cn.dankal.flyaudio.personal.EditAddressActivity;

public class AddressManagerAdapter extends BaseRecyclerViewAdapter<AddressEntity> {

    private HandleListener listener;

    public void setListener(HandleListener listener) {
        this.listener = listener;
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_address_manager;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new AddressManagerViewHolder(rootView);
    }

    public class AddressManagerViewHolder extends BaseRecyclerViewHolder<AddressEntity> {

        public AddressManagerViewHolder(View itemView) {
            super(itemView);

        }

        @Override
        public void onBindData(AddressEntity data, int position) {
            setVisibility(R.id.tv_address_default, data.getDefaultAddress());
            setText(R.id.tv_address_person, "收货人:" + data.getContacter());
            setText(R.id.tv_address_person_mobile, data.getPhone());
            setText(R.id.tv_address_person_address, data.getProvince() + data.getCity() + data.getRegion() + data.getAddressDetail());
            itemView.findViewById(R.id.rl_content).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener !=null){
                        listener.onItemClick(data);
                    }
                }
            });
            itemView.findViewById(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ActivityUtils.getTopActivity(), AddAddressActivity.class);
                    intent.putExtra("address", data);
                    ActivityUtils.startActivity(intent);
                }
            });
            itemView.findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener !=null){
                        listener.delete(position);
                    }
                }
            });
            itemView.findViewById(R.id.tv_default).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener !=null){
                        listener.setDefault(position);
                    }
                }
            });
        }
    }

    public interface HandleListener {

        void setDefault(int pos);

        void delete(int pos);

        void onItemClick(AddressEntity data);

    }
}
